package com.xiaodu.acer.phoneplay.ui.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.context.IconicsLayoutInflater;
import com.mikepenz.ionicons_typeface_library.Ionicons;
import com.xiaodu.acer.phoneplay.App;
import com.xiaodu.acer.phoneplay.R;
import com.xiaodu.acer.phoneplay.di.component.AppComponent;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import library.base.BaseView;

/**
 * Name: 杜志斌 1392229149
 * Time: 2017/4/21.
 * Dec:
 * Activity 的基类
 * 实现 MVP模式 配合多模块Activty+多Fragment
 * 全局的ToolBar
 */

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity
        implements BaseView {

    private Unbinder mUnbinder;

    protected App mApplication;

    private View mRootView;


    @Inject
    protected P mPresenter;

    protected Toolbar mToolBar;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
        super.onCreate(savedInstanceState);

        if (mRootView == null) {
            mRootView = View.inflate(this, R.layout.template_toolbar_framelayout, null);
            //放入base布局到ContentView中
            setContentView(mRootView);


            mToolBar = mRootView.findViewById(R.id.tool_bar);
            //如果图标不一样，可以重写覆盖此方法
            mToolBar.setNavigationIcon(new IconicsDrawable(this)
                    .icon(Ionicons.Icon.ion_ios_arrow_back)
                    .sizeDp(14)
                    .color(getResources().getColor(R.color.md_white_1000)));

            //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()
            //Fragment中添加布局
            FrameLayout mContent = mRootView.findViewById(R.id.content);
            mContent.addView(View.inflate(this, setLayout(), null));
            mUnbinder = ButterKnife.bind(this, mRootView);

            this.mApplication = (App) getApplication();

            setupAcitivtyComponent(mApplication.getAppComponent());

            initView();
            initData();
            initListener();
        } else {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (parent != null) {
                parent.removeView(mRootView);
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mUnbinder != Unbinder.EMPTY) {

            mUnbinder.unbind();
        }
    }

    public abstract int setLayout();

    public abstract void setupAcitivtyComponent(AppComponent appComponent);

    public abstract void initView();

    public abstract void initData();

    public abstract void initListener();

    @Override
    public void showLoading() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void dismissLoading() {

    }


}
